//
//  LMTabBar.m
//  LMTabBarControllerKit
//
//  Created by mengmenglu on 6/25/16.
//  Copyright © 2016 Hangzhou TaiXuan Network Technology Co., Ltd. All rights reserved.
//

#import "LMTabBar.h"
#import "Masonry.h"

@interface LMTabBar ()

@property (nonatomic, strong) NSMutableArray *tabBarItems;
@property (nonatomic,strong) UIImageView *separatorImageView;

@end

@implementation LMTabBar

#pragma mark - LifeCycle
- (instancetype)init {
    self = [super init];
    if (self) {
        [self initParam];
        [self initView];
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initParam];
        [self initView];
    }
    return self;
}


#pragma mark - Public Method
- (void)updateTabBarItemsWith:(NSArray *)tabBarItems {
    if (![tabBarItems count] > 0) {
        NSLog(@"更新TabbarItem数据失败");
        return;
    } else {
        [self removeOldTabBarItems];
        _tabBarItems = [tabBarItems copy];
        [self configTabBarViews:_tabBarItems];
    }
}

#pragma mark - Event Response
- (void)tabBarItemSelectClick:(LMTabBarItem *)tabBarItem { // 本来当前选中状态等于上一选中状态是不需要改变状态的
    
    for (LMTabBarItem *tabBarItem in self.tabBarItems) {
        tabBarItem.selected = NO;
    }
    
    tabBarItem.selected = YES;
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabBar:selectedItem:index:)]) {
        [self.delegate tabBar:self selectedItem:tabBarItem index:[self.tabBarItems indexOfObject:tabBarItem]];
    }
}


#pragma mark - Private Method
- (void)initView {
    
    self.backgroundColor = [UIColor whiteColor];
    
    // 添加分割线
    [self addSubview:self.separatorImageView];
    
    // 添加TabBarItem
    [self configTabBarViews:self.tabBarItems];
}


- (void)initParam {
    LMTabBarItem *tabBarItem1 = [[LMTabBarItem alloc] init];
    LMTabBarItem *tabBarItem2 = [[LMTabBarItem alloc] init];
    LMTabBarItem *tabBarItem3 = [[LMTabBarItem alloc] init];
    LMTabBarItem *tabBarItem4 = [[LMTabBarItem alloc] init];
    self.tabBarItems =  [NSMutableArray arrayWithArray:@[tabBarItem1,tabBarItem2,tabBarItem3,tabBarItem4]];
}


- (void)configTabBarViews:(NSArray *)tabBarItems {
    NSInteger itemCount = [tabBarItems count];
    CGFloat itemWidth = (self.frame.size.width / itemCount);
    for (NSInteger i = 0 ; i < itemCount ; i++) {
        [self setTabBarItemWithIndex:i itemWidth:itemWidth];
    }
}


- (void)setTabBarItemWithIndex:(NSInteger)index itemWidth:(CGFloat)itemWidth {
    LMTabBarItem *tabBarItem = self.tabBarItems[index];
    [tabBarItem updateItemView];
    tabBarItem.frame = CGRectMake(itemWidth*index, 0, itemWidth, TABBARITEM_HEIGHT);
    [tabBarItem addTarget:self action:@selector(tabBarItemSelectClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:tabBarItem];
    if (index == 0) {
        tabBarItem.selected = YES;
    }
}


- (void)removeOldTabBarItems {
    for (LMTabBarItem *tabBarItem in self.tabBarItems) {
        [tabBarItem removeFromSuperview];
    }
    [self.tabBarItems removeAllObjects];
}


#pragma mark - Gettter Method
- (UIImageView *)separatorImageView {
    if (!_separatorImageView) {
        _separatorImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _separatorImageView.image = [UIImage imageNamed:@"general_grey_line_top"];
    }
    return _separatorImageView;
}



@end
